<?php


//产品类别
function get_product_category() {
	
	$categorys = $GLOBALS['db']->getAll('select * from kyz_products_cate');
	return $categorys;
}

//取得产品
function get_products() { 
	$products = $GLOBALS['db']->getAll("select * from kyz_products");
	return $products;
}

//热门产品
function get_hot() { 
	$hot_products = $GLOBALS['db']->getAll("select * from kyz_products order by views desc limit 6");
	return $hot_products;
}

//推荐产品
function get_recommend() { 
	$recommend_products = $GLOBALS['db']->getAll("select * from kyz_products where recommend=1 order by create_time desc limit 4");
	return $recommend_products;
}

//最近更新
function get_new() { 
	$new_products = $GLOBALS['db']->getAll("select * from kyz_products order by create_time desc limit 6");
	return $new_products;
}

//相关产品
function get_relative($cate_id) { 
	$relative_products = $GLOBALS['db']->getAll("select * from kyz_products where cate_id='$cate_id' limit 4");
	return $relative_products;
}

/**
 * 操作提示信息
 */
function show_message($msg, $url){
	global $smarty;
	$smarty->assign('msg', $msg);
	$smarty->assign('url', $url);
	$smarty->display('admin/show_message.html');
}

/**
 * 生成缩略图
*/
function ImageResize($srcFile,$toW,$toH,$toFile=""){
	if($toFile==""){
    	$toFile = $srcFile;
     }
    $info = "";
    $data = GetImageSize($srcFile,$info);
    switch ($data[2]){
	   case 1:
	    if(!function_exists("imagecreatefromgif")){
	     	echo "你的GD库不能使用GIF格式的图片，请使用Jpeg或PNG格式！<a href='javascript:go(-1);'>返回</a>";
	     	exit();
	    }
	    $im = ImageCreateFromGIF($srcFile);
	    break;
	   case 2:
	    if(!function_exists("imagecreatefromjpeg")){
	     	echo "你的GD库不能使用jpeg格式的图片，请使用其它格式的图片！<a href='javascript:go(-1);'>返回</a>";
	     	exit();
	    }
	    $im = ImageCreateFromJpeg($srcFile);   
	    break;
	   case 3:
	    $im = ImageCreateFromPNG($srcFile);   
	    break;
   }
   $srcW=ImageSX($im);
   $srcH=ImageSY($im);
   $toWH=$toW/$toH;
   $srcWH=$srcW/$srcH;
   /*
   if($toWH<=$srcWH){
     $ftoW=$toW;
     $ftoH=$ftoW*($srcH/$srcW);
   }else{
    $ftoH=$toH;
    $ftoW=$ftoH*($srcW/$srcH);
   } 
   */
   $ftoW = $toW;
   $ftoH = $toH; 
   if($srcW>$toW||$srcH>$toH){
   	if(function_exists("imagecreatetruecolor")){
    	@$ni = ImageCreateTrueColor($ftoW,$ftoH);
    	if($ni){
    		ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
    	}else{
    		$ni=ImageCreate($ftoW,$ftoH);
     		ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
    		}
   }else{
    $ni=ImageCreate($ftoW,$ftoH);
    ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
   }
   if(function_exists('imagejpeg')){
   	ImageJpeg($ni,$toFile);
   }else ImagePNG($ni,$toFile);
   	ImageDestroy($ni);
   }
   ImageDestroy($im);
} 

/**
 * 生成随机数
*/
function randomkeys($length) {
	$pattern = '1234567890';
	for($i = 0; $i < $length; $i++) {
		$key .= $pattern{mt_rand(0,9)};
	}
	return $key;
}

function pages($totle, $pageSize = 20, $url = '') {
	global $page, $firstcount, $pagenav, $_SERVER;
	if (!$page) {
		$page = 1;
	}
  	if (!$url) {
   		$url = $_SERVER["REQUEST_URI"];
  	}
	$parse_url = parse_url($url);
  	$url_query = $parse_url["query"];
  	if ($url_query) {
   		$url_query = ereg_replace("(^|&)page=$page", "", $url_query);
   		$url = str_replace($parse_url["query"], $url_query, $url);
   		if ($url_query) {
    		$url .= "&page";
   		}else {
    		$url .= "page";
   		}
  	} else {
   		$url .= "?page";
  	}
  	$total_page = ceil($totle / $pageSize);
    $page = min($total_page, $page);
    $prepg = $page -1; //上一页
    $nextpg = ($page == $total_page ? 0 : $page +1); //下一页
    $firstcount = ($page -1) * $pageSize;
    $pagenav = "显示第 <B>" . ($totle ? ($firstcount +1) : 0) . "</B>-<B>" . min($firstcount + $pageSize, $totle) . "</B> 条记录，共 $totle 条记录";
	if ($total_page <= 1) {
   		return false;
	}
  	$pagenav .= " <a href='$url=1'>首页</a> ";
  	if ($prepg) {
   		$pagenav .= " <a href='$url=$prepg'>上一页</a> ";
  	}else {
   		$pagenav .= " 上一页 ";
  	}
  	if($nextpg) {
   		$pagenav .= " <a href='$url=$nextpg'>下一页</a> ";
  	}else {
   		$pagenav .= " 下一页 ";
  	}
  	$pagenav .= " <a href='$url=$total_page'>尾页</a> ";
  	/*
	$pagenav .= "　到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
  	for ($i = 1; $i <= $total_page; $i++) {
   		if ($i == $page) {
    		$pagenav .= "<option value='$i' selected>$i</option>\n";
   		} else {
    		$pagenav .= "<option value='$i'>$i</option>\n";
   		}
  	}
  	$pagenav .= "</select> 页，共 $total_page 页";
  	*/
  	$pagenav .= "<input type='text' size=5 id='gopage'><input type='button' value='跳转' onclick='window.location=\"$url=\"+document.getElementById(\"gopage\").value'>";
  	$pagenav .= "共 $total_page 页";
  	return $pagenav;
}


/**
 * *
 *等比缩放
 * @param unknown_type $srcImage   源图片路径
 * @param unknown_type $toFile     目标图片路径
 * @param unknown_type $maxWidth   最大宽
 * @param unknown_type $maxHeight  最大高
 * @param unknown_type $imgQuality 图片质量
 * @return unknown
 */

function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
{
 
    list($width, $height, $type, $attr) = getimagesize($srcImage);
    //if($width < $maxWidth  || $height < $maxHeight) return ;
    switch ($type) {
    case 1: $img = imagecreatefromgif($srcImage); break;
    case 2: $img = imagecreatefromjpeg($srcImage); break;
    case 3: $img = imagecreatefrompng($srcImage); break;
    }
    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例
   
   // if($scale < 1) {
    $newWidth = floor($scale*$width);
    $newHeight = floor($scale*$height);
    $newImg = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $newName = "";
    $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);

    switch($type) {
        case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
        return "$newName.gif"; break;
        case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
        return "$newName.jpg"; break;
        case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
        return "$newName.png"; break;
        default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
        return "$newName.jpg"; break;
    }
    imagedestroy($newImg);
   // }
    imagedestroy($img);
    return false;
}


?>
